package com.mdd.common.util.sms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class Sms {

	public static void main(String[] args) {
        //短信下发
        String sendUrl = "https://smssh1.253.com/msg/v1/send/json";
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("account","N6350001");//API账号
        map.put("password","ovPtrNSjk116da");//API密码
        map.put("msg","【快点嘛】您的验证码是123456，请勿告知他人，5分钟内有效。");//短信内容
        map.put("phone","18565675728");//手机号
        map.put("report","true");//是否需要状态报告
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        System.out.println(sendSmsByPost(sendUrl,js.toString()));
       
    }
	
	public static String sendSmsCode(String code,String phone) {
		 String sendUrl = "https://smssh1.253.com/msg/v1/send/json";
	        Map<String,Object> map = new HashMap<String,Object>();
	        map.put("account","N6350001");//API账号
	        map.put("password","ovPtrNSjk116da");//API密码
	        map.put("msg","【快点嘛】您的验证码是"+code+"，请勿告知他人，30分钟内有效。");//短信内容
	        map.put("phone",phone);//手机号
	        map.put("report","true");//是否需要状态报告
	        JSONObject js = (JSONObject) JSONObject.toJSON(map);
	        String sendSmsByPost = sendSmsByPost(sendUrl,js.toString());
			System.out.println(sendSmsByPost);
	        JSONObject parseObject = JSONObject.parseObject(sendSmsByPost);
	        if("0".equals(parseObject.getString("code"))) {
	        	return "0";
	        }
	        return parseObject.getString("errorMsg");
	}
	
    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
